package casanet.activity;

import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import casanet.parser.ParserArticle;
import casanet.ui.android.Menara;
import casanet.ui.android.R;


public class Details extends Activity implements OnClickListener {

	private ImageButton mButton;
	private int indexTab ;

	public void onCreate(Bundle savedInstanceState) {
			
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.details);
		Bundle extras = getIntent().getExtras();

		if (extras != null) {
			ParserArticle article = (ParserArticle)extras.get("article");
			indexTab = (int)extras.getInt("indexe");
			((TextView)findViewById(R.id.d_titre)).setText(article.getTitre());
			((TextView)findViewById(R.id.d_source)).setText(article.getDate()+", "+article.getSource()+"\n");
			String value ="";// /images/avion%20ram_P.jpg";
			int length = article.getImageP().length();
			if(article.getImageP().length()>0){
				value = article.getImageM().substring(20,length);
			}
			try {
				URL aURL = new URL("http://41.248.240.28"+ value); 
				URLConnection conn = aURL.openConnection(); 
				conn.connect(); 
				InputStream is = conn.getInputStream(); 
				Bitmap here_comes_it = BitmapFactory.decodeStream(is);
				((ImageView)findViewById(R.id.d_imgM)).setImageBitmap(here_comes_it);
			} catch (Exception e) {
				// TODO: handle exception
			}			
			((TextView)findViewById(R.id.d_description)).setText(article.getDescription());
		}
		mButton = (ImageButton)findViewById(R.id.ButtonRetour);
		mButton.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		if (v.equals(this.mButton)) {
			Intent intent= new Intent(getApplicationContext(),Menara.class);
			intent.putExtra("indexe", indexTab);
			startActivity(intent);
		}

	}


}

